package com.wujunchao.demo.service.impl;

import com.wujunchao.demo.pojo.Student;
import com.wujunchao.demo.mapper.StudentMapper;
import com.wujunchao.demo.service.StudentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 学生表(Student)表服务实现类
 *
 * @author wn
 * @version 1.0
 * @since 2021-07-02 14:52:12
 */
@Service("studentService")
public class StudentServiceImpl implements StudentService {
    @Resource
    private StudentMapper studentMapper;

    /**
     * 根据模糊条件查询总个数
     *
     * @return 返回查询到的总个数
     */
    @Override
    public Map<String, Object> chaXunCount(String mingCheng) {
        Map<String, Object> map = new HashMap<>();

        map.put("code", 200);
        map.put("msg", "查询成功");
        map.put("list", this.studentMapper.chaXunCount(mingCheng));
        return map;
    }


    /**
     * 查询所有数据
     *
     * @return 返回所有数据
     */
    @Override
    public Map<String, Object> chaXunAll() {
        Map<String, Object> map = new HashMap<>();

        map.put("code", 0);
        map.put("data", this.studentMapper.chaXunAll());
        return map;
    }

    /**
     * 通过ID查询单条数据
     *
     * @param sno 主键
     * @return 实例对象
     */
    @Override
    public Map<String, Object> chaXunById(Integer sno) {
        Map<String, Object> map = new HashMap<>();

        map.put("code", 200);
        map.put("msg", "查询成功");
        map.put("obj", this.studentMapper.chaXunById(sno));
        return map;
    }

    /**
     * 查询分页数据
     *
     * @param page      查询起始位置
     * @param mingCheng 查询条数
     * @return 对象列表
     */
    @Override
    public Map<String, Object> chaXunFenYe(int page, String mingCheng) {
        // 获取当前表中的总记录
        int tableCount = this.studentMapper.chaXunCount(mingCheng);
        int pageCount = (tableCount - 1) / 10 + 1;
        // 计算每页开始的下标值
        int xiaBiao = (page - 1) * 10;
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "查询成功");
        map.put("pageCount", pageCount);
        map.put("count", tableCount);     // 当前表中的总条数
        map.put("data", this.studentMapper.chaXunFenYe(xiaBiao, mingCheng));
        return map;
    }

    /**
     * 新增数据
     *
     * @param student 实例对象
     * @return 实例对象
     */
    @Override
    public Map<String, Object> xinZeng(Student student) {
        // UUID.randomUUID()  返回内容：asd21321-ewrewrew213213-123213zsad-123asdasd这样的形态
        this.studentMapper.xinZeng(student);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "新增成功");
        return map;
    }

    /**
     * 通过ID查询单条数据
     *
     * @param student 实例对象
     * @return 实例对象
     */
    @Override
    public Map<String, Object> gengXinById(Student student) {
        this.studentMapper.gengXinById(student);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "更新成功");
        return map;
    }

    @Override
    public boolean login(String username, String password) {
        Student login = studentMapper.login(username, password);
        if (login!=null){
            return true;
        }else {
            return false;
        }
    }

    /**
     * 通过主键删除数据
     *
     * @param sno 主键
     * @return 是否成功
     */
    @Override
    public Map<String, Object> shanChuById(String sno) {
        this.studentMapper.shanChuById(sno);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "删除成功");
        return map;
    }
}
